package com.common.frame.listener

import android.animation.Animator
import android.view.View

class AnimatorListener(callback: ListenerBuilder.() -> Unit) : Animator.AnimatorListener {

    val listener = ListenerBuilder().also(callback)

    override fun onAnimationStart(animation: Animator) {
        listener.onAnimationStart?.invoke(animation)
    }

    override fun onAnimationEnd(animation: Animator) {
        listener.onAnimationEnd?.invoke(animation)
    }

    override fun onAnimationCancel(animation: Animator) {
        listener.onAnimationCancel?.invoke(animation)
    }

    override fun onAnimationRepeat(animation: Animator) {
        listener.onAnimationRepeat?.invoke(animation)
    }

    class ListenerBuilder {
        var onAnimationStart: ((Animator) -> Unit)? = null
        var onAnimationEnd: ((Animator) -> Unit)? = null
        var onAnimationCancel: ((Animator) -> Unit)? = null
        var onAnimationRepeat: ((Animator) -> Unit)? = null

        fun onAnimationStart(action: (Animator) -> Unit) {
            onAnimationStart = action
        }

        fun onAnimationEnd(action: (Animator) -> Unit) {
            onAnimationEnd = action
        }

        fun onAnimationCancel(action: (Animator) -> Unit) {
            onAnimationCancel = action
        }

        fun onAnimationRepeat(action: (Animator) -> Unit) {
            onAnimationRepeat = action
        }
    }
}